package com.setting.box

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.os.Bundle
import android.provider.Settings
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.tencent.mmkv.MMKV
import com.tools.box.setting.databinding.ActivityCommonAboutBinding

class CommonAboutActivity : AppCompatActivity() {

    var bindingF: ActivityCommonAboutBinding? = null
    val binding get() = bindingF!!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        bindingF = ActivityCommonAboutBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.backIv.setOnClickListener {
            finish()
        }
        binding.titleTv.text = "关于我们"
        binding.versionTv.text = "V${com.setting.box.setting.util.SystemUtilTool.getVersionName(this)}"

        binding.logo.setOnClickListener {
            getConfig()
        }

        binding.logo.setOnLongClickListener {
            getAndroidIdAndOaid()
            return@setOnLongClickListener true
        }
        binding.versionTv.setOnClickListener {
            changeConfig()
        }
        MMKV.initialize(this)
    }

    private var getConfigClickCount = 0
    private var changeConfigClickCount = 0
    private val COMMON = "lanch:commonstatus"
    private val XUNJIAN = "lanch:isxunjian"
    private val SH = "lanch:shstatus"

    private fun getAndroidIdAndOaid() {
        val androidId = getAndroidId(this@CommonAboutActivity);
        val oaId = getOaId();
        val copy = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
        if (copy != null) {
            val mClipData = ClipData.newPlainText("Label", androidId + "\n" + oaId)
            copy.setPrimaryClip(mClipData)
        }
        Log.e("CommonAboutActivity", "androidId==> $androidId")
        Log.e("CommonAboutActivity", "oaId==> $oaId")
    }

    private fun getConfig() {
        getConfigClickCount++
        if (getConfigClickCount >= 10) {
            getConfigClickCount = 0
            val configs = "isXunjian=>${isXunjian()} , isCommon=>${isCommon()} , isSh=>${isSh()}"
            val copy = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
            if (copy != null) {
                val mClipData = ClipData.newPlainText("Label", configs)
                copy.setPrimaryClip(mClipData)
            }
            Log.e("CommonAboutActivity", configs)
        }
    }

    private fun changeConfig() {
        changeConfigClickCount++
        if (changeConfigClickCount >= 10) {
            changeConfigClickCount = 0
            val starConfigs = "starConfigs isXunjian=>${isXunjian()} , isCommon=>${isCommon()} , isSh=>${isSh()}"
            Log.e("CommonAboutActivity", starConfigs)
            setCommonStatus(true)
            val endConfigs = "endConfigs isXunjian=>${isXunjian()} , isCommon=>${isCommon()} , isSh=>${isSh()}"
            Log.e("CommonAboutActivity", endConfigs)
        }
    }


    private fun isXunjian(): Boolean {
        return MMKV.defaultMMKV().decodeBool(XUNJIAN, false)
    }

    private fun isCommon(): Boolean {
        return MMKV.defaultMMKV().decodeBool(COMMON, false)
    }

    private fun isSh(): Boolean {
        return MMKV.defaultMMKV().decodeBool(SH, true)
    }


    private fun getAndroidId(context: Context): String? {
        val androidId =
                try {
                    Settings.System.getString(context.contentResolver,
                            Settings.Secure.ANDROID_ID)
                } catch (throwable: Throwable) {
                    ""
                }
        return androidId
    }

    private fun getOaId(): String? {
        return MMKV.defaultMMKV().decodeString("device:oaid", "")
    }

    private fun setCommonStatus(status: Boolean) {
        clearStatus()
        MMKV.defaultMMKV().encode(COMMON, status)
    }

    private fun clearStatus() {
        // 审核
        MMKV.defaultMMKV().encode(SH, false)
        // 正常
        MMKV.defaultMMKV().encode(COMMON, false)
        // 巡检
        MMKV.defaultMMKV().encode(XUNJIAN, false)
    }

}